package com.kalaqia.im.library.room;

import android.view.View;

import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMChatRoomManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;

import dagger.Module;
import dagger.Provides;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * function:
 * project:kalaqia
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2017/8/16 16:26
 */
//<editor-fold desc="version update log">
// updare owner:kalaqia  421407393@qq.com
// update datetime:2017/8/16 16:26 
// update remark:
//</editor-fold>
@Module
public class ChatRoomModule {

    private String roomId;
    private Observable<String> sendMsgObservable;

    public ChatRoomModule(String roomId) {
        this.roomId = roomId;
    }

    public ChatRoomModule(String roomId, Observable<String> sendMsgObservable) {
        this.roomId = roomId;
        this.sendMsgObservable = sendMsgObservable;
    }

    @Provides
    public Observable<String> provideSendMsgObservable() {
        return this.sendMsgObservable;
    }

    @Provides
    public Observable<EMChatRoomManager> provideEMChatRoomManagerObservable() {
        return Observable.just(EMClient.getInstance().chatroomManager());
    }

    @Provides
    public Observable<EMChatRoom> provideJoinChatRoomObservable(Observable<EMChatRoomManager> observable) {
        return observable.concatMap(emChatRoomManager -> new EMChatRoomJoinObservable(emChatRoomManager, roomId));
    }

    @Provides
    public Observable<EMMessage> provideMsgObservable(Observable<String> observable) {
        return observable.map(content -> {
            EMMessage message = EMMessage.createTxtSendMessage(content, roomId);
            message.setChatType(EMMessage.ChatType.ChatRoom);
            return message;
        });
    }

}
